$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/borderinfo/sysUserFollow',
        datatype: "json",
        colModel: [
            { label: '用户ID', name: 'userId', index: 'user_id',align: "center", width: 50 },
            { label: '用户名称', name: 'username', index: 'username',align: "center", width: 80},
            { label: '跟进次数', name: 'followCount', index: 'follow_count',align: "center", width: 80 },
            { label: '成交订单数', name: 'dealedCount', index: 'dealed_count',align: "center", width: 80},
            { label: '成交订单积分', name: 'dealedScore', index: 'dealed_score',align: "center", width: 80},
            { label: '总积分', name: 'totalScore', index: 'total_score',align: "center", width: 80 ,
                formatter:function(value,options,rowData){
                    return '<span style="margin-right: 10px">'+value+'</span><img src="../../statics/dist/public/image/userFollowScore.jpg" width="20px" height="20px" />';
                }
            },
            { label: '详情', name: 'detail', index: '',align: "center", width: 80, hidden: true,
                formatter:function(value,options,rowData){
                    let currentUserId = rowData["userId"];
                    return '<a class="btn btn-primary" onclick="vm.showDetail('+currentUserId+')">详情</a>';
                }
            }
        ],
        viewrecords: true,
        height: 520,
        rowNum: 10,
        rowList : [10,20,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        // postData:{"userId":vm.q.userId,"userName":vm.q.userName,"gzhCode":vm.q.gzhCode,"beginDate":vm.q.beginDate,"endDate":vm.q.endDate},
        postData:{"userId":vm.q.userId,"userName":vm.q.userName,"beginDate":vm.q.beginDate,"endDate":vm.q.endDate},
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });

    /*$.ajax({
        type: "POST",
        async:false,
        url: baseURL + 'sys/buserinfo/getUserGzhList',
        contentType: "application/json",
        success: function(r){
            if(r.code==0){
                $("#selectUserGzhList").append(
                    "<option value=''>全部平台</option>");
                for(var i=0;i<r.userGzhList.length;i++){
                    $("#selectUserGzhList").append(
                        "<option value=" + r.userGzhList[i].gzhCode +">"+ r.userGzhList[i].gzhName+"</option>");
                }
            }
        }
    });*/

    vm.initSizeDetail();
    $(window).resize(function(){
        vm.resizeDetail();
    });
});

var vm = new Vue({
    el: "#app",
    data: {
        q: {
            userId: null,
            userName: null,
            gzhCode: '',
            beginDate: null,
            endDate: null
        },
        showList: true,
        width: null,
        height: null,
        selectedUserId: null,
        layero: null
    },
    mounted() {
        let now = new Date();

        /*let y = now.getFullYear(); //年
        let m = now.getMonth() + 1 + ''; //月

        m = m.length == 1 ? '0' + m : m;
        this.q.beginDate = y + '-' + m + '-01';
        //本月的结束时间
        let monthEndDate = new Date(y, now.getMonth()+1, 0);
        this.q.endDate = y + '-' + m + '-' + monthEndDate.getDate();*/

        let weekFirstDay = new Date(now - (now.getDay() - 1) * 86400000);
        let weekLastDay = new Date((weekFirstDay / 1000 + 6 * 86400) * 1000);

        /*let y1 = weekFirstDay.getFullYear();
        let m1 = weekFirstDay.getMonth() + 1 + '';
        m1 = m1.length == 1 ? '0' + m1 : m1;
        let d1 = weekFirstDay.getDate() + '';
        d1 = d1.length == 1 ? '0' + d1 : d1;

        let y2 = weekLastDay.getFullYear();
        let m2 = weekLastDay.getMonth() + 1 + '';
        m2 = m2.length == 1 ? '0' + m2 : m2;
        let d2 = weekLastDay.getDate() + '';
        d2 = d2.length == 1 ? '0' + d2 : d2;

        this.q.beginDate =  y1 + '-' + m1 + '-' + d1;
        this.q.endDate = y2 + '-' + m2 + '-' + d2;*/

        this.q.beginDate = formateDate(weekFirstDay);
        this.q.endDate = formateDate(weekLastDay);
        console.log(this.q.beginDate);
        console.log(this.q.endDate);

        function formateDate(date) {
            let year = date.getFullYear();
            let month = date.getMonth() + 1;
            month = month < 10 ? '0' + month : month;
            let day = date.getDate();
            day = day < 10 ? '0' + day : day;
            return year + "-" + month + "-" +day;
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        showDetail: function (e) {
            vm.selectedUserId = e;
            layer.open({
                type: 1,
                title: ['当前用户ID：'+vm.selectedUserId, 'font-style: weight'],
                area: [ vm.width+'px', vm.height+'px'],
                shadeClose: true,
                scrollbar: false,
                content: $('#layer'),
                success: function (layero, index) {
                    vm.layero = layero;
                }
            });
            vm.reloadDetail();
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page,
                // postData:{"userId":vm.q.userId,"userName":vm.q.userName,"gzhCode":vm.q.gzhCode,"beginDate":vm.q.beginDate,"endDate":vm.q.endDate},
                postData:{"userId":vm.q.userId,"userName":vm.q.userName,"beginDate":vm.q.beginDate,"endDate":vm.q.endDate},
            }).trigger("reloadGrid");
        },
        reloadDetail: function (event) {
            vm.showList = true;
            var page = $("#jgGridDetail").jqGrid('getGridParam','page');
            $("#jgGridDetail").jqGrid('setGridParam',{
                page:1,
                // postData:{"userId":vm.selectedUserId,"gzhCode":vm.q.gzhCode,"beginDate":vm.q.beginDate,"endDate":vm.q.endDate},
                postData:{"userId":vm.selectedUserId,"beginDate":vm.q.beginDate,"endDate":vm.q.endDate},
            }).trigger("reloadGrid");
        },
        initSizeDetail: function() {
            // 初始化宽高值，用于定义弹窗的大小
            vm.width = $(window).width() - 200;
            vm.height = $(window).height() - 200;
        },
        resizeDetail: function () {
            vm.initSizeDetail();

            // layer 自适应
            if (vm.layero) {
                vm.layero.css("width", vm.width + "px");
                vm.layero.css("height", vm.height + "px");
            }
            vm.setGridSize();
        },
        setGridSize: function () {
            $("#jgGridDetail").jqGrid("setGridWidth", vm.width - 18);
            $("#jgGridDetail").jqGrid("setGridHeight", vm.height - 124);
        }
    }
});

$(function () {
    $("#jgGridDetail").jqGrid({
        url: baseURL + 'sys/borderinfo/followInfoByUserId',
        datatype: "json",
        colModel: [
            { label: '订单号', name: 'orderId', index: 'order_id',align: "center", width: 110},
            { label: '用户ID', name: 'userId', index: 'user_id',align: "center", width: 50},
            { label: '用户昵称', name: 'userName', index: 'userName',align: "center", width: 80 },
            { label: '点击支付(次)', name: 'orderCount', index: 'orderCount',align: "center", width: 60,
                formatter: function (value, options, rowData) {
                    let count = rowData["count"];
                    if (count > 2) {
                        return "<p style='color: red'>" + value + "</>";
                    } else {
                        return "<p'>" + value + "</>";
                    }
                }
            },
            { label: '访问会员页(次)', name: 'pageCount', index: 'pageCount',align: "center" , width: 65},
            { label: '品牌', name: 'brandName', index: 'brandName',align: "center", width: 60 },
            { label: '公众号', name: 'gzhName', index: 'gzhName',align: "center", width:50 },
            { label: '交易金额', name: 'totalAmount', index: 'total_amount',align: "center", width: 50 },
            { label: '交易说明', name: 'itemName', index: 'itemName',align: "center", width: 50},
            { label: '交易时间', name: 'createTime', index: 'create_time',align: "center", width: 95 },
            /*{ label: '跟进状态', name: 'followStatus', index: 'follow_status',align: "center", width: 50,
                formatter:function(value,options,rowData){
                    if(value==0){
                        return "未跟进";
                    }else if(value==1){
                        return "已跟进";
                    }
                }
            },
            { label: '跟进人', name: 'followUserName', index: 'followUserName',align: "center", width: 80 },*/
            { label: '跟进时间', name: 'followTime', index: 'follow_time',align: "center", width: 95 },
            { label: '跟进时长(分钟)', name: 'followDuration', index: 'follow_duration',align: "center", width: 70 },
            { label: '跟进积分', name: 'followScore', index: 'follow_score',align: "center", width: 60 },
            { label: '成交订单积分', name: 'transStatus', index: 'trans_status',align: "center", width: 70 },
        ],
        viewrecords: true,
        height: 520,
        rowNum: 10,
        rowList : [10,20,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPagerDetail",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        // postData:{"userId":vm.selectedUserId,"gzhCode":vm.q.gzhCode,"beginDate":vm.q.beginDate,"endDate":vm.q.endDate},
        postData:{"userId":vm.selectedUserId,"beginDate":vm.q.beginDate,"endDate":vm.q.endDate},
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jgGridDetail").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            vm.setGridSize();
        }
    });
})

$(function () {
    // 获取用户是否有赠送 vip 权限 sys:buserinfo:givevip
    $.ajax({
        type: "GET",
        url: baseURL + "sys/user/hasPermission",
        contentType: "application/json;charset=utf-8",
        data: {"permission": "sys:buserinfo:givevip"},
        success: function (r) {
            if (r.code == 0) {
                // 如果用户有赠送VIP的权限，则显示详情列;否则，不显示。
                if (r.isPermitted) {
                    $("#jqGrid").showCol("detail");
                } else {
                    $("#jqGrid").hideCol("detail");
                }
            }
        }
    })
})
